package com.hw.controller.basic;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hw.entity.dto.basic.FoodDTO;
import com.hw.entity.query.basic.FoodQuery;
import com.hw.entity.vo.basic.FoodVO;
import com.hw.model.ResponseResult;
import com.hw.service.basic.FoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 菜品
 *
 * @author whw
 * @date 2021/6/24
 */
@Api(tags = "菜品")
@RestController()
@RequestMapping("/food")
public class FoodController {
    @Autowired
    FoodService foodService;

    @ApiOperation(value = "获取列表")
    @GetMapping("/list")
    public ResponseResult<List<FoodVO>> list(FoodQuery query) {
        return ResponseResult.success(foodService.list(query));
    }

    @ApiOperation(value = "保存图片")
    @PostMapping("/saveImg")
    public ResponseResult<?> saveImg(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "foodId") String foodId) {
        foodService.updateAvatar(file, foodId);
        return ResponseResult.success();
    }

    @ApiOperation(value = "保存")
    @PostMapping("/save")
    public ResponseResult<?> save(@RequestBody FoodDTO dto) {
        return foodService.save(dto);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    public ResponseResult<?> delete(String foodId) {
        return foodService.delete(foodId);
    }

    @ApiOperation(value = "获取详情")
    @GetMapping("/getDetail")
    public ResponseResult<?> getDetail(String foodId) {
        if (null == foodService.getDetail(foodId)) {
            ResponseResult.error("获取失败！");
        }
        return ResponseResult.success(foodService.getDetail(foodId));
    }

    @ApiOperation("下载导入模板")
    @GetMapping("/downloadImportTemplate")
    public ResponseResult<?> downloadImportTemplate() {
        return ResponseResult.success("/import/food/菜品导入模板.xls");
    }


    @ApiOperation("导入数据")
    @PostMapping("/import")
    public ResponseResult<?> importData(@RequestParam("file")MultipartFile file) {
        return foodService.importData(file);
    }
}
